package com.sec.etech.common;


import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.rest.util.RequestUtil;
import org.openbpm.bus.model.BusinessData;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Iterator;

@Component
@Aspect
public class SaveAopAction {


    @Pointcut("execution(* org.openbpm.form.rest.controller.FormDefDataController.saveData(..))")
    private void controllerMethodAspect() {
    }


    public LifeCycle getLifeCycle(String key){
        return SpringUtil.getBean(key, LifeCycle.class);
    }

//    @Before("controllerMethodAspect()")
//    public void doBefore() {
//        System.out.println("aop---------------------开始");
//    }


    @Around("controllerMethodAspect()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        HttpServletRequest request = (HttpServletRequest) args[0];
        HttpServletResponse response = (HttpServletResponse) args[1];
        JSONObject data = (JSONObject) args[2];

        String key = RequestUtil.getString(request, "key");

        LifeCycle lifeCycle = getLifeCycle(key);

        Object obj = null;

        if (lifeCycle != null){
            if (lifeCycle.checkPermission(request, response, data)){

                lifeCycle.beforeSave(request, response, data);
                try {
                    obj =  pjp.proceed(args);
                }catch (DuplicateKeyException ex){
                    String s = ex.getMessage();
                    int i = s.indexOf("Duplicate entry");
                    int j = s.indexOf("'", i + 16);
                    StringBuilder sb = new StringBuilder();
                    sb.append("出现重复的输入值：");
                    if (i > 0){
                        sb.append(s.substring(i + 15, j + 1));
                    }
                    sb.append(", 请修改后提交");
                    throw new Exception(sb.toString());
                }
                ResultMsg r = (ResultMsg) obj;
                JSONObject resultData = (JSONObject) r.getData();
                Iterator it = resultData.keySet().iterator();
                Object object = resultData.get(it.next().toString());
                if (object instanceof BusinessData){
                    BusinessData busData = (BusinessData) object;
                    data.put("dbData", busData.getDbData());
                    lifeCycle.afterSave(request, response, data);
                }else if(object instanceof BigDecimal || object instanceof Long || object instanceof String){
                    data.put("bdpk", object);
                    lifeCycle.afterSave(request, response, data);
                } else {
                    lifeCycle.afterSave(request, response, data);
                }
            }else{
                throw new Exception(lifeCycle.permissionDenied());
            }

        }else {
            obj =  pjp.proceed(args);
        }

        return obj;
    }

}
